﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Microsoft.Win32;
using System.IO;
using System.Text.RegularExpressions;

namespace ConvertAseMap
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{
		public App()
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "ASE 3D File (.ASE)|*.ASE";
			Nullable<bool> openResult = openFileDialog.ShowDialog();

			if (openResult != true)
			{
				return;
			}

			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "Navigation Map (.XML)|*.XML";
			Nullable<bool> saveResult = saveFileDialog.ShowDialog();

			if (openResult == true)
			{
				string sourceFileName = openFileDialog.FileName;
				string destinationFileName = saveFileDialog.FileName;
				using (FileStream
					source = File.Open(sourceFileName, FileMode.Open),
					destination = File.Open(destinationFileName, FileMode.Create))
				{
					StreamReader reader = new StreamReader(source);
					StreamWriter writer = new StreamWriter(destination);

					writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
					writer.WriteLine("<XnaContent>");
					writer.WriteLine("\t<Asset Type=\"GameEngine.Navigation.NavigationMesh\">");
					writer.WriteLine("\t\t<Vertices>");

					Regex vertexRegex = new Regex(@"\s*\*MESH_VERTEX\s+(?<index>[^\s]*)\s+(?<X>[^\s]*)\s+(?<Y>[^\s]*)\s+(?<Z>[^\s]*)");
					Regex faceRegex = new Regex(@"\s*\*MESH_FACE\s+(?<index>[^:]*):\s*A:\s*(?<A>[^\s]*)\s*B:\s*(?<B>[^\s]*)\s*C:\s*(?<C>[^\s]*)\s*AB:\s*(?<AB>[^\s]*)\s*BC:\s*(?<BC>[^\s]*)\s*CA:\s*(?<CA>[^\s]*)\s*\*MESH_SMOOTHING\s*(?<Groups>[^\s]*)");
					// parse file here...

					bool isPrintingVerices = true;

					while (!reader.EndOfStream)
					{
						string line = reader.ReadLine();
						// proccess next line...
						Match match;
						if (isPrintingVerices)
						{
							match = vertexRegex.Match(line);
							if (match.Success)
							{
								writer.WriteLine("\t\t\t{0} {1} {2}", match.Groups["X"], match.Groups["Y"], match.Groups["Z"]);
								continue;
							}
						}
						match = faceRegex.Match(line);
						if (match.Success)
						{
							if (isPrintingVerices)
							{
								isPrintingVerices = false;
								writer.WriteLine("\t\t</Vertices>");
								writer.WriteLine("\t\t<Faces>");
							}
							writer.WriteLine("\t\t\t<Item>");
							writer.WriteLine("\t\t\t\t<A>{0}</A>", match.Groups["A"]);
							writer.WriteLine("\t\t\t\t<B>{0}</B>", match.Groups["B"]);
							writer.WriteLine("\t\t\t\t<C>{0}</C>", match.Groups["C"]);
							writer.WriteLine("\t\t\t\t<Groups>{0}</Groups>", match.Groups["Groups"].ToString().Replace(',', ' '));
							writer.WriteLine("\t\t\t</Item>");
						}
					}

					writer.WriteLine("\t\t</Faces>");
					writer.WriteLine("\t</Asset>");
					writer.WriteLine("</XnaContent>");

					writer.Flush();
					destination.Flush();
				}
			}
		}
	}
}
